<template>
  <div>
    <el-card>
      <el-col :span="4" style="right: auto">
        <el-button type="primary" clearable icon="el-icon-plus" @click="openEditUI(null)">新增</el-button>
      </el-col>
    </el-card>
    <!--结果列表-->
    <el-card id="list">
      <el-table
        :data="errorproofList"
        stripe
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="编号"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="errorTerm"
          label="错误项"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
        prop="type"
        label="错误类型"
        align="center"
        width="200"
      >
      </el-table-column>

        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" circle></el-button>
            <el-button @click="delStandardById(scope.row)" type="danger" icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--分页组件-->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchModel.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--  用户信息编辑对话框  -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="errorproofForm" :rules="rules" ref="errorproofFormRef">
        <el-form-item label="错误项" prop="errorTerm" :label-width="formLabelWidth">
          <el-input v-model="errorproofForm.errorTerm" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="错误类型" prop="type" :label-width="formLabelWidth">
          <el-input v-model="errorproofForm.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="导致结果" prop="result" :label-width="formLabelWidth">
          <el-input v-model="errorproofForm.result" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预防手段" prop="preventive" :label-width="formLabelWidth">
          <el-input v-model="errorproofForm.preventive" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveList">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import errorproofApi from '@/api/errorproof'
export default {
  name: 'errorproof',
  data() {
    return {
      formLabelWidth:'200px',//控制'用户名'到左边的宽度
      errorproofForm:{},
      dialogFormVisible:false,//控制对话框是否可见
      title:'',
      total:0,
      searchModel: {
        pageNum:1,
        pageSize: 10
      },
      errorproofList: [],
      rules:{
        errorTerm: [
          { required: true, message: '错误项不能为空', trigger: 'blur' },
          { min: 6, max: 50, message: '长度在 6 到 50 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '错误类型不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        result: [
          { required: true, message: '导致结果不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        preventive: [
          { required: true, message: '预防手段不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],

      }
    }
  },
  methods:{
    saveList(){
      //触发表单验证
      this.$refs.errorproofFormRef.validate((valid) => {
        if (valid) {
          //提交请求给后台
          errorproofApi.saveList(this.errorproofForm).then(response =>{
            //成功提示
            this.$message({
              message:response.message,
              type:'success'
            });
            //关闭对话框
            this.dialogFormVisible = false;
            //刷新表格
            this.getErrorproofList();
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    clearForm(){
      //清理表单
      this.errorproofForm = {};
      this.$refs.errorproofFormRef.clearValidate();//清除校验结果
    },
    openEditUI(id){
      //打开编辑界面
      if (id == null){
        this.title='添加质检标准';
      }else{
        this.title='修改质检标准';
        //根据id查询数据
        errorproofApi.getErrorproofById(id).then(response =>{
          this.errorproofForm = response.data;
        })
      }
      this.dialogFormVisible=true;
    },
    handleSizeChange(pageSize){
      this.searchModel.pageSize = pageSize;
      this.getErrorproofList();
    },
    handleCurrentChange(pageNum){
      this.searchModel.pageNum = pageNum;
      this.getErrorproofList();
    },
    getErrorproofList() {
      const info = {
        pageNum: this.searchModel.pageNum,
        pageSize: this.searchModel.pageSize,
      }
      errorproofApi.getErrorproofList(this.searchModel).then(response => {
        this.errorproofList = response.data.records;
        this.total = response.data.total;
      });
    },
    delStandardById(errorproof){
      this.$confirm(`您确认删除流水号为${errorproof.serialNumber}的数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        errorproofApi.delStandardById(errorproof.id).then(response =>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getErrorproofList();

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  created() {
    this.getErrorproofList();
  }
}
</script>

<style scoped>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input{
  width: 80%;
}
</style>
